package com.zxx.admin.config.mybatis;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *  mybatis-plus自动填充功能
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime = this.getFieldValByName("createTime", metaObject);
        Object updateTime = this.getFieldValByName("updateTime", metaObject);
        Object createUser = this.getFieldValByName("createUser", metaObject);
        Object updateUser = this.getFieldValByName("updateUser", metaObject);
        Date currTime = new Date();
        if (null == createTime) {
            metaObject.setValue("createTime", currTime);
        }
        if (null == updateTime) {
            metaObject.setValue("updateTime", currTime);
        }
        if (createUser == null) {
            metaObject.setValue("createUser","默认创建者");
        }
        if (updateUser == null) {
            metaObject.setValue("updateUser", "默认修改者");
        }
        metaObject.setValue("delFlag", "0");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateTime", new Date(), metaObject);
        setFieldValByName("updateUser", "默认修改者", metaObject);
    }


}
